<?php
namespace User\Controller;
use Common\Controller\MemberbaseController;
class HuodongController extends MemberbaseController {
	
	
	protected $db;
	function _initialize(){
		parent::_initialize();

		$this->db=D("Huodong");
	}





	function baoming(){


		if(IS_POST){

			$uid=$this->u["id"];
			$id=I('post.id');
			$shop_id=I('post.shop_id');
			$name=I('post.name');
			$tel=I('post.tel');
			$address=I('post.address');



			$info=$this->db->get_one($id,$uid);


			if(!$tel){

				$this->error("请选择收货地址");
			}




			switch($info["jiedian"]){

				case 2:
					$this->error("抱歉,礼品已经领完!");
					break;
				case 3:
					$this->error("抱歉,活动已经结束");
					break;
				case 4:
					$this->error("请支付1元",U('User/Order/pay',array("order_sn"=>$info["order_sn"])));
					break;
				case 5:
					$this->error("您已经参加过了");
					break;



			}

			$time=time();






			$order_sn=date('ymd').substr(time(),-5).substr(microtime(),2,5);


			$order=array(
				"uid"=>$uid,
				"tel"=>$tel,
				"title"=>$info["product"],
				"shop_id"=>$shop_id,
				"address"=>$address,
				"name"=>$name,
				"price_sum"=>1,
				"qb_price_sum"=>1,
				"status"=>1,
				"inputtime"=>$time,
				"modal"=>"huodong",
				"ip"=>ip2long(get_client_ip()),
				"order_sn"=>$order_sn,
				"gl_id"=>"$id",


			);

			D("Order")->add($order);



			$data=array(
				"order_sn"=>$order_sn,
				"num"=>$info["xian_num"],
				"product_name"=>$info["product"],
			);


			D("OrderInfo")->add($data);



			$this->success("报名成功!",U('User/Order/pay',array("order_sn"=>$order_sn)));



		}else{



			$id=I('get.id');
			$uid=$this->u["id"];
			$info=$this->db->get_one($id,$uid);

			switch($info["jiedian"]){

				case 2:
					$this->error("抱歉,礼品已经领完!");
					break;
				case 3:
					$this->error("抱歉,活动已经结束");
					break;
				case 4:
					$this->error("您已经参加过了");
					break;



			}

			cookie("address_self",get_now_url());

			$address=D("Address")->where(array("uid"=>$uid))->order("'default'  desc")->select();

			$this->assign("default_address",$address[0]);
			$this->assign("address",$address);
			$this->assign("info",$info);
			$this->display();
		}




	}




}